/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import SistemaProyectoVuelos.SistemaUsuario;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import proyectovuelos.CiudadImpl;

/**
 *
 * @author Usuario
 */
public class JInternalFramePorgVueloCiudades extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFramePorgVueloCiudades
     */
    public JInternalFramePorgVueloCiudades() {
        initComponents();
        setSize(614, 345); 
        setResizable(false); 
        setVisible(true);
        setLocation(565,20);
        setTitle("Servicio Programar Vuelo-Ciudades");
        
        DefaultComboBoxModel modeloCombo  = new DefaultComboBoxModel();
       
       modeloCombo.addElement("ISO");
       modeloCombo.addElement("Nombre");
       modeloCombo.addElement("idCiudad");
       jComboBox1.setModel(modeloCombo);
       
       DefaultComboBoxModel modeloCombo2  = new DefaultComboBoxModel();
       
       modeloCombo2 = SistemaProyectoVuelos.SistemaProgVuelo.cargarCombo2();
       jComboBox2.setModel(modeloCombo2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Programacion Ciudades");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jLabel2.setText("Seleccione Tema de busqueda");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jLabel3.setText("Ciudad");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jLabel4.setText("País");

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jButton2.setText("Update");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jButton3.setText("Delete");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jButton4.setText("Limpiar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jLabel5.setText("idCiudad");

        jButton6.setFont(new java.awt.Font("Tahoma", 0, 17)); // NOI18N
        jButton6.setText("Modificar");
        jButton6.setToolTipText("");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Habilitar Campos");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton3)
                                .addGap(64, 64, 64)
                                .addComponent(jButton4)
                                .addGap(100, 100, 100))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jButton6)
                                    .addGap(48, 48, 48)
                                    .addComponent(jButton2)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(174, 174, 174)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jButton7)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton6))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         Connection conn = null;
        Statement st = null;
        
        try {
            
            Class.forName("org.gjt.mm.mysql.Driver");
            conn  = DriverManager.getConnection ("jdbc:mysql://localhost:3306/proyectovuelos?user=root&password=");
            st = conn.createStatement();
            int cantidad = st.executeUpdate("INSERT INTO paises (ISO, Nombre)  VALUES ('" +jComboBox2.getSelectedItem()+ "' , '" + jTextField2.getText() + "')");
            
            if (cantidad==1) {
                JOptionPane.showMessageDialog(null, "Se pudo dar de alta la Ciudad Correctamente");
          } else {
                JOptionPane.showMessageDialog(null, "NO se pudo dar de alta la Ciudad");
          }
            
                jButton5.setText("");
		jTextField2.setText(""); 
                jTextField1.setText(""); 

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "NO se pudo traer la Ciudad de la Base de Datos. ERORR!!");
        } finally {
            try {
                //rs.close();
                st.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(JInternalAdministrador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
                
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
          
        ResultSet rs = null;
        Connection conn = null;
        Statement st = null;
        
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conn  = DriverManager.getConnection ("jdbc:mysql://localhost:3306/proyectovuelos?user=root&password=");
            st = conn.createStatement();
            rs = st.executeQuery("SELECT * FROM paises WHERE Nombre='"+jTextField1.getText()+"'");
            
            if (rs.next()==true) {
		jButton5.setText(rs.getString("idCiudad"));
		jTextField2.setText(rs.getString("Nombre"));
                jComboBox2.setSelectedItem(rs.getString("ISO"));

                jButton5.setEnabled(false);
		jTextField2.setEnabled(false);
                jComboBox2.setEnabled(false);
                
            } else {
                JOptionPane.showMessageDialog(null, "NO existe Ciudad en la BD");
            }  
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "NO se pudo traer la Ciudad de la Base de Datos. ERORR!!");
        } finally {
            try {
                rs.close();
                st.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(JInternalAdministrador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
                jButton5.setText("");
		jTextField2.setText("");
                jTextField1.setText(""); 
                
                jButton5.setEnabled(true);
		jTextField2.setEnabled(true);
                jTextField1.setEnabled(true);
                jComboBox2.setEnabled(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        Connection conn = null;
        Statement st = null;
        
        try {
            
            Class.forName("org.gjt.mm.mysql.Driver");
            conn  = DriverManager.getConnection ("jdbc:mysql://localhost:3306/proyectovuelos?user=root&password=");
            st = conn.createStatement();
            int cantidad = st.executeUpdate("UPDATE paises SET ISO='" +jComboBox2.getSelectedItem() + "', Nombre='" + jTextField2.getText() + "' WHERE Nombre= '"+jTextField1.getText()+"'");
            
            if (cantidad==1) {
                JOptionPane.showMessageDialog(null, "Se modifico la Ciudad Correctamente");
          } else {
                JOptionPane.showMessageDialog(null, "NO se pudo modificar la Ciudad Usuario");
          }
            
                jButton5.setText("");
		jTextField2.setText(""); 
                jTextField1.setText(""); 

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "NO se pudo traer la Ciudad de la Base de Datos. ERORR!!");
        } finally {
            try {
                //rs.close();
                st.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(JInternalAdministrador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
                
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Connection conn = null;
        Statement st = null;
        
        try {
            
            Class.forName("org.gjt.mm.mysql.Driver");
            conn  = DriverManager.getConnection ("jdbc:mysql://localhost:3306/proyectovuelos?user=root&password=");
            
            //conn.setAutoCommit(false);
            st = conn.createStatement();
           int cantidad = st.executeUpdate("DELETE FROM paises WHERE Nombre='" + jTextField1.getText() + "'");
            
            //st.executeUpdate("INSERT INTO paisesbaja DELETE * FROM paises");
            
            //conn.commit();
           
            if (cantidad==1) {
                JOptionPane.showMessageDialog(null, "Se dio de baja a dicha Ciudad Correctamente!! Transaccion realizada con exito");
          } else {
                JOptionPane.showMessageDialog(null, "NO se pudo dar de baja a dicha Ciudad");
          }
            
                jButton5.setText("");
		jTextField2.setText(""); 
                jTextField1.setText(""); 
                
                jButton5.setEnabled(true);
		jTextField2.setEnabled(true); 
                jTextField1.setEnabled(true); 

        } catch (Exception e) {
            try {
                JOptionPane.showMessageDialog(null, "NO se pudo traer el Usuario de la Base de Datos. ERORR!!");
               // conn.rollback();
            } catch (Exception ex) {
                Logger.getLogger(JInternalFramePorgVueloCiudades.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
            try {
               // conn.setAutoCommit(true);
                st.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(JInternalAdministrador.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
                jButton5.setEnabled(true);
		jTextField2.setEnabled(true);
                jComboBox2.setEnabled(true);
    }//GEN-LAST:event_jButton7ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
